import { defineStore } from "pinia";

/**
 * 用户Model的静态数据
 */
const defState = {
  collectList: uni.getStorageSync("jyfw-collectList") || [],
  collectKeyList: uni.getStorageSync("jyfw-collectKeyList") || [],
  collectResultList: uni.getStorageSync("jyfw-collectResultList") || [],
  collectResultKeyList: uni.getStorageSync("jyfw-collectResultKeyList") || [],
  // 表格数据
  listData: {
    records: [],
    applyLength: 0,
  },
  // 表格配置
  listConfig: {
    scopeIds: [],
  },
  userInfo: uni.getStorageSync("userInfo") || {},
  version: {
    number: "2.3.3",
    content: "1.去掉条目详情中事项的适用区域",
  },
};

/**
 * 用户相关仓库
 */
export const user = defineStore({
  id: "user",
  state: () => ({
    ...defState,
  }),
  actions: {
    collectItem(payload: any) {
      uni.showToast({
        title: "已存入收藏夹",
        icon: "none",
      });
      this.collectList = payload.collectList;
      this.collectKeyList = payload.collectKeyList;
      uni.setStorageSync("jyfw-collectList", this.collectList);
      uni.setStorageSync("jyfw-collectKeyList", this.collectKeyList);
    },
    removeCollectItem(payload: { index: number }) {
      uni.showToast({
        title: "已移除收藏",
        icon: "none",
      });
      uni.setStorageSync("jyfw-collectList", this.collectList);
      uni.setStorageSync("jyfw-collectKeyList", this.collectKeyList);
    },
    resetUserInfo() {
      this.userInfo = "";
    },
    save(payload: any) {
      this.collectList = payload.collectList || [];
      this.collectKeyList = payload.collectKeyList || [];
      this.collectResultList = payload.collectResultList || [];
      this.collectResultKeyList = payload.collectResultKeyList || [];
      this.listData = payload.listData;
      this.listConfig = payload.listConfig;
      this.userInfo = payload.userInfo;
      this.version = payload.version;
    },
  },
});
